//
//  OMTFlowLayoutLabel.m
//  OMTFlowLayoutLabel
//
//  Created by yizhaorong on 2018/9/8.
//

#import "OMTFlowLayoutLabel.h"
#import "OMTFlowLayout.h"
#import "OMTFlowLayoutLabelCell.h"

static CGFloat const kMinimumSpacing = 10;

static NSInteger const kDefaultMaxRow = 3;

@interface OMTFlowLayoutLabel () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, OMTFlowLayoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) OMTFlowLayout *flowLayout;

@end

@implementation OMTFlowLayoutLabel

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _font = [UIFont boldSystemFontOfSize:14];
        _textColor = [UIColor whiteColor];
        _backgroundColor = [UIColor colorWithRed:135/255.0 green:206/255.0 blue:235/255.0 alpha:1];
        _labelHeight = 24;
        _maxRow = kDefaultMaxRow;
        _backgroundImage = nil;
        
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    [self addSubview:self.collectionView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.flowLayout.labelHeight = self.labelHeight;
    CGRect frame = self.bounds;
    self.collectionView.frame = frame;
    CGFloat height = self.collectionView.contentSize.height;
    CGFloat maxH = self.maxRow * self.labelHeight + self.flowLayout.sectionInset.top + self.flowLayout.sectionInset.bottom + (self.maxRow - 1) * self.flowLayout.minimumLineSpacing;
    frame.size.height = MIN(height, maxH);
    self.collectionView.frame = frame;
    frame.origin = self.frame.origin;
    self.frame = frame;
}

#pragma mark - Public
- (void)reloadData {
    [self.collectionView reloadData];
    [self.superview setNeedsLayout];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OMTFlowLayoutLabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.title = self.datas[indexPath.row];
    cell.font = self.font;
    cell.textColor = self.textColor;
    cell.backgroundImage = self.backgroundImage;
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(flowLayoutLabel:didSelectLabelAtIndex:)]) {
        [self.delegate flowLayoutLabel:self didSelectLabelAtIndex:indexPath.row];
    }
    
    if (self.didSelectLabelAtIndexBlock) {
        self.didSelectLabelAtIndexBlock(self, indexPath.row);
    }
}

#pragma mark - OMTFlowLayoutDelegate
- (CGFloat)flowLayout:(OMTFlowLayout *)flowLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    static dispatch_once_t onceToken;
    static UILabel *label;
    dispatch_once(&onceToken, ^{
        label = [UILabel new];
    });
    label.font = self.font;
    label.text = self.datas[indexPath.row];
    CGFloat maxW = CGRectGetWidth(self.collectionView.frame) - kMinimumSpacing * 2 - 32;
    CGSize size = [label sizeThatFits:CGSizeMake(maxW , self.labelHeight)];
    return 32 + size.width;
}

#pragma mark - Setters And Getters

- (void)setMaxRow:(NSInteger)maxRow {
    if (maxRow < 1) {
        maxRow = kDefaultMaxRow;
    }
    _maxRow = maxRow;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:OMTFlowLayoutLabelCell.class forCellWithReuseIdentifier:@"cell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (OMTFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [OMTFlowLayout new];
        _flowLayout.delegate = self;
        _flowLayout.sectionInset = UIEdgeInsetsMake(kMinimumSpacing, kMinimumSpacing, kMinimumSpacing, kMinimumSpacing);
        _flowLayout.minimumLineSpacing = kMinimumSpacing;
        _flowLayout.minimumInteritemSpacing = kMinimumSpacing;
    }
    return _flowLayout;
}

@end
